﻿#define XML
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Web.SessionState;

[WebService ( Namespace = "http://tempuri.org/" )]
[WebServiceBinding ( ConformsTo = WsiProfiles.BasicProfile1_1 )]
[ScriptService]
public class repeater_webservice : WebService, IRequiresSessionState
{

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> FillBookList ( int pageindex, int pagesize )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

#if XML
		int itemcount;
		DataTable bookTable = SampleHelper.Filter ( null, "ID asc", beginIndex, endIndex, null, "Book", out itemcount );
#else
		int itemcount = SampleHelper.QueryValue ( "select count(*) from book" );
		DataTable bookTable = SampleHelper.ExecuteQuery ( string.Format ( "select ID, BookName, Author, Publisher, PublishDate, Price, Discount, Rank, RN from (select ID, BookName, Author, Publisher, PublishDate, Price, Discount, Rank, (select count(*) from book where ID <= X.ID) as RN from book as X order by ID asc) as Y where RN >= {0} and RN <= {1}", beginIndex, endIndex ), "Book" ).Book;
#endif

		List<object> tableJSON = SampleHelper.CreateJSONArray ( bookTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "bookname": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> FillMailList ( int pageindex, int pagesize, string __order, string __group )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

		int itemcount;
		DataTable mailTable = SampleHelper.Filter ( null, ( __order == string.Empty ? string.Empty : __order + "," ) + "ReceiveDate desc", beginIndex, endIndex, null, "QQMail", out itemcount );

		List<object> tableJSON = SampleHelper.CreateJSONArray ( mailTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "sender": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> FillStudentList ( int pageindex, int pagesize )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

		string where = string.Empty;

#if XML
		int itemcount;
		DataTable studentTable = SampleHelper.Filter ( null, "ID asc", beginIndex, endIndex, null, "Student", out itemcount );
#else
		int itemcount = SampleHelper.QueryValue ( "select count(*) from Student where (1 = 1)" + where );
		DataTable studentTable = SampleHelper.ExecuteQuery ( string.Format ( "select ID, RealName, Birthday, Sex, Major, Grade, RN from (SELECT ID, RealName, Birthday, Sex, Major, Grade, (select count(*) from Student where ID <= X.ID" + where + ") as RN from Student as X where (1 = 1)" + where + " order by ID asc) as Y where RN >= {0} and RN <= {1}", beginIndex, endIndex ), "Student" ).Student;
#endif

		List<object> tableJSON = SampleHelper.CreateJSONArray ( studentTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "realname": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> SearchGoogle ( int pageindex, int pagesize )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

#if XML
		int itemcount;
		DataTable googleTable = SampleHelper.Filter ( null, "ID asc", beginIndex, endIndex, null, "Google", out itemcount );
#else
		int itemcount = SampleHelper.QueryValue ( "select count(*) from google" );
		DataTable googleTable = SampleHelper.ExecuteQuery ( string.Format ( "select ID, Title, Url, UpdatedDate, Remark, Picture, RN from (SELECT ID, Title, Url, UpdatedDate, Remark, Picture, (select count(*) from book where ID <= X.ID) as RN from google as X order by ID asc) as Y where RN >= {0} and RN <= {1}", beginIndex, endIndex ), "Google" ).Google;
#endif

		List<object> tableJSON = SampleHelper.CreateJSONArray ( googleTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "title": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> GetGooglePicture ( string url )
	{
		this.Context.Response.Cache.SetNoStore ( );

#if XML
		int itemcount;
		DataTable googleTable = SampleHelper.Filter ( string.Format ( "Url = '{0}'", url ), "ID asc", 1, 1, null, "Google", out itemcount );
#else
		int itemcount = 1;
		DataTable googleTable = SampleHelper.ExecuteQuery ( string.Format ( "select ID, Title, Url, UpdatedDate, Remark, Picture from Google where Url = '{0}'", url ), "Google" ).Google;
#endif

		List<object> tableJSON = SampleHelper.CreateJSONArray ( googleTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "title": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> GetOrders ( int pageindex, int pagesize )
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

		int itemcount = SampleHelper.QueryValue ( "select count(*) from [Order]" ); ;
		DataTable orderTable = SampleHelper.ExecuteQuery ( string.Format ( "select ID, Buyer, Address, Product, Amount, OrderDate, IsCompleted, Sum, Price, RN from (select ID, Buyer, Address, Product, Amount, OrderDate, IsCompleted, Sum, Price, (select count(*) from [Order] where ID <= X.ID) as RN from [Order] as X order by ID asc) as Y where RN >= {0} and RN <= {1}", beginIndex, endIndex ), "Order" ).Order;

		List<object> tableJSON = SampleHelper.CreateJSONArray ( orderTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount ),
			new KeyValuePair<string, object> ( "custom", SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
				new KeyValuePair<string, object> ( "message", string.Format("获取了第 {0} 页的数据", pageindex) )
			}
			)),
			}
			);

	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> ModifyOrder ( int id, string buyer, string address, string product, DateTime orderdate, short amount, float price, float sum, bool iscompleted )
	{
		this.Context.Response.Cache.SetNoStore ( );

		SampleDSTableAdapters.OrderTableAdapter adapter = new SampleDSTableAdapters.OrderTableAdapter ( );

		try
		{ adapter.Update ( buyer, address, product, amount, orderdate, iscompleted, sum, price, id ); }
		catch { }

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "custom", SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
				new KeyValuePair<string, object> ( "message", string.Format("修改了编号为 {0} 的订单, 服务器时间 {1}", id, DateTime.Now) )
			}
			))
			}
			);

		/*
		 * {
		 *  "__success": true,
		 *  "custom": { "message": "..." }
		 * }
		 * */
	}

	[WebMethod]
	[ScriptMethod]
	public SortedDictionary<string, object> CreateOrder ( string buyer, string address, string product, DateTime orderdate, short amount, float price, float sum, bool iscompleted )
	{
		this.Context.Response.Cache.SetNoStore ( );

		SampleDSTableAdapters.OrderTableAdapter adapter = new SampleDSTableAdapters.OrderTableAdapter ( );

		try
		{ adapter.Insert ( buyer, address, product, amount, orderdate, iscompleted, sum, price ); }
		catch { }

		int id = SampleHelper.QueryValue("select max(id) from [Order]");

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "row", SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
				new KeyValuePair<string, object> ( "id", id ),
				new KeyValuePair<string, object> ( "buyer", buyer ),
				new KeyValuePair<string, object> ( "address", address ),
				new KeyValuePair<string, object> ( "product", product ),
				new KeyValuePair<string, object> ( "orderdate", orderdate ),
				new KeyValuePair<string, object> ( "amount", amount ),
				new KeyValuePair<string, object> ( "price", price ),
				new KeyValuePair<string, object> ( "iscompleted", iscompleted ),
				new KeyValuePair<string, object> ( "sum", sum )
			}
			)),
			new KeyValuePair<string, object> ( "custom", SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
				new KeyValuePair<string, object> ( "message", string.Format("新建了订单, 服务器时间 {0}", DateTime.Now) )
			}
			))
			}
			);

	}

}